package com.ymatou.iapi.userservice.testcase;

import java.security.interfaces.RSAPublicKey;
import java.util.Base64;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.couponservice.parameter.invalidCouponBean;
import com.ymatou.iapi.userservice.parameter.TradingPasswordRequestDtoBean;
import com.ymatou.iapi.userservice.parameter.UpdateTradingPwdBean;
import com.ymatou.iapi.userservice.parameter.req.CreateUserReq;
import com.ymatou.iapi.userservice.service.CreateUserCall;
import com.ymatou.iapi.userservice.service.TradingPasswordRequestDtoCall;
import com.ymatou.iapi.userservice.service.UpdateTradingPwdCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.core.tool.RSAUtils;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.utils.DesUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
/**
*验证交易密码Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_TradingPasswordRequestDto{
	private static TradingPasswordRequestDtoBean passwordrequestdtoBean;
	private static TradingPasswordRequestDtoCall passwordrequestdtoCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("验证交易密码Test");
	}

	@Before
	public void caseUp() {
		passwordrequestdtoBean=new TradingPasswordRequestDtoBean();
		passwordrequestdtoCall=new TradingPasswordRequestDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(passwordrequestdtoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_TradingPasswordRequestDto_001() {
		Logger.start(true,"验证交易密码,成功");
		try {
			CreateUserReq cur = new CreateUserReq();
		    cur = JUserCallService.initCreateUserBean(cur);
		    Integer uid = JUserCallService.createUserCall(cur);
		    
		    UpdateTradingPwdBean updadetradingpwdbean = new UpdateTradingPwdBean();
		    UpdateTradingPwdCall updatetradingpwdcall = new UpdateTradingPwdCall();
		    updadetradingpwdbean.setAType("ByUserId");
		    updadetradingpwdbean.setLoginMobile(cur.getMobile());
		    updadetradingpwdbean.setNewPassword("abc123");
		    updadetradingpwdbean.setOldPassword(null);
		    updadetradingpwdbean.setPasActionType("ModifyTradingPassword");
		    updadetradingpwdbean.setUserId(uid);
		    updatetradingpwdcall.setData(updadetradingpwdbean);
		    updatetradingpwdcall.callService();
	
			passwordrequestdtoBean.setUserId(uid);
			passwordrequestdtoBean.setNewPassword("abc123");
			passwordrequestdtoCall.setData(FormatUtil.beanToHttpString(passwordrequestdtoBean,true,false));
			//passwordrequestdtoCall.setData(passwordrequestdtoBean);
			passwordrequestdtoCall.callService();			
			
			Logger.verifyEquals("1", passwordrequestdtoCall.getString("Result"), "验证Result");
			Logger.verifyEquals("true", passwordrequestdtoCall.getString("Success"), "验证Success");		
			Logger.verifyEquals("200", passwordrequestdtoCall.getString("ResponseCode"), "验证ResponseCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_TradingPasswordRequestDto_002() {
		Logger.start(true,"交易密码为空验证交易密码,失败");
		try {
			CreateUserReq cur = new CreateUserReq();
		    cur = JUserCallService.initCreateUserBean(cur);
		    Integer uid = JUserCallService.createUserCall(cur);		    
	
			passwordrequestdtoBean.setUserId(uid);
			passwordrequestdtoBean.setNewPassword("abc123");
			passwordrequestdtoCall.setData(FormatUtil.beanToHttpString(passwordrequestdtoBean,true,false));
			//passwordrequestdtoCall.setData(passwordrequestdtoBean);
			passwordrequestdtoCall.callService();			
			
			Logger.verifyEquals("-1", passwordrequestdtoCall.getString("Result"), "验证Result");
			Logger.verifyEquals("false", passwordrequestdtoCall.getString("Success"), "验证Success");		
			Logger.verifyEquals("400", passwordrequestdtoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyIsContains("无交易密码", passwordrequestdtoCall.getString("LastErrorMessage"), "验证LastErrorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_TradingPasswordRequestDto_003() {
		Logger.start(true,"交易密码错误验证交易密码,失败");
		try {
			CreateUserReq cur = new CreateUserReq();
		    cur = JUserCallService.initCreateUserBean(cur);
		    Integer uid = JUserCallService.createUserCall(cur);
		    
		    UpdateTradingPwdBean updadetradingpwdbean = new UpdateTradingPwdBean();
		    UpdateTradingPwdCall updatetradingpwdcall = new UpdateTradingPwdCall();
		    updadetradingpwdbean.setAType("ByUserId");
		    updadetradingpwdbean.setLoginMobile(cur.getMobile());
		    updadetradingpwdbean.setNewPassword("abc123");
		    updadetradingpwdbean.setOldPassword(null);
		    updadetradingpwdbean.setPasActionType("ModifyTradingPassword");
		    updadetradingpwdbean.setUserId(uid);
		    updatetradingpwdcall.setData(updadetradingpwdbean);
		    updatetradingpwdcall.callService();
	
			passwordrequestdtoBean.setUserId(uid);
			passwordrequestdtoBean.setNewPassword(JUserCallService.defaultLoginPwd);
			passwordrequestdtoCall.setData(FormatUtil.beanToHttpString(passwordrequestdtoBean,true,false));
			//passwordrequestdtoCall.setData(passwordrequestdtoBean);
			passwordrequestdtoCall.callService();			
			
			Logger.verifyEquals("false", passwordrequestdtoCall.getString("Success"), "验证Success");		
			Logger.verifyEquals("0", passwordrequestdtoCall.getString("Result"), "验证Result");
			Logger.verifyIsContains("用户交易密码错误", passwordrequestdtoCall.getString("LastErrorMessage"), "验证LastErrorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_TradingPasswordRequestDto_004() {
		Logger.start(true,"uid为null验证交易密码,失败");
		try {
			CreateUserReq cur = new CreateUserReq();
		    cur = JUserCallService.initCreateUserBean(cur);
		    Integer uid = JUserCallService.createUserCall(cur);		    
	
			//passwordrequestdtoBean.setUserId(null);
			passwordrequestdtoBean.setNewPassword("abc123");
			passwordrequestdtoCall.setData(FormatUtil.beanToHttpString(passwordrequestdtoBean,true,false));
			//passwordrequestdtoCall.setData(passwordrequestdtoBean);
			passwordrequestdtoCall.callService();			
			
			Logger.verifyEquals("false", passwordrequestdtoCall.getString("Success"), "验证Success");		
			Logger.verifyEquals("400", passwordrequestdtoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyIsContains("userid must gte", passwordrequestdtoCall.getString("LastErrorMessage"), "验证LastErrorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_TradingPasswordRequestDto_005() {
		Logger.start(true,"不传入交易密码,失败");
		
		try {
			CreateUserReq cur = new CreateUserReq();
		    cur = JUserCallService.initCreateUserBean(cur);
		    Integer uid = JUserCallService.createUserCall(cur);		    
	
			passwordrequestdtoBean.setUserId(uid);
			//passwordrequestdtoBean.setNewPassword("abc123");
			passwordrequestdtoCall.setData(FormatUtil.beanToHttpString(passwordrequestdtoBean,true,false));
			//passwordrequestdtoCall.setData(passwordrequestdtoBean);
			passwordrequestdtoCall.callService();			
			
			Logger.verifyEquals("-1", passwordrequestdtoCall.getString("Result"), "验证Result");
			Logger.verifyEquals("false", passwordrequestdtoCall.getString("Success"), "验证Success");		
			Logger.verifyEquals("400", passwordrequestdtoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyIsContains("无交易密码", passwordrequestdtoCall.getString("LastErrorMessage"), "验证LastErrorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_TradingPasswordRequestDto_006() {
		Logger.start(true,"验证交易密码,密码加密,成功");
		try {
			CreateUserReq cur = new CreateUserReq();
		    cur = JUserCallService.initCreateUserBean(cur);
		    Integer uid = JUserCallService.createUserCall(cur);
		    
		    UpdateTradingPwdBean updadetradingpwdbean = new UpdateTradingPwdBean();
		    UpdateTradingPwdCall updatetradingpwdcall = new UpdateTradingPwdCall();
		    updadetradingpwdbean.setAType("ByUserId");
		    updadetradingpwdbean.setLoginMobile(cur.getMobile());
		    updadetradingpwdbean.setPasswordType(1);
		    String pwd = DesUtil.encrypt("abc123",DesUtil.DES_KEY_STRING,DesUtil.DES_IVPARAMETER);
		    updadetradingpwdbean.setNewPassword(pwd);
		    updadetradingpwdbean.setOldPassword(null);
		    updadetradingpwdbean.setPasActionType("ModifyTradingPassword");
		    updadetradingpwdbean.setUserId(uid);
		    updatetradingpwdcall.setData(updadetradingpwdbean);
		    updatetradingpwdcall.callService();
	
			passwordrequestdtoBean.setUserId(uid);
			passwordrequestdtoBean.setNewPassword("abc123");
			passwordrequestdtoCall.setData(FormatUtil.beanToHttpString(passwordrequestdtoBean,true,false));
			//passwordrequestdtoCall.setData(passwordrequestdtoBean);
			passwordrequestdtoCall.callService();			
			
			Logger.verifyEquals("1", passwordrequestdtoCall.getString("Result"), "验证Result");
			Logger.verifyEquals("true", passwordrequestdtoCall.getString("Success"), "验证Success");		
			Logger.verifyEquals("200", passwordrequestdtoCall.getString("ResponseCode"), "验证ResponseCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_TradingPasswordRequestDto_007() {
		Logger.start(true,"验证交易密码,有旧密码密码加密,成功");
		try {
			CreateUserReq cur = new CreateUserReq();
		    cur = JUserCallService.initCreateUserBean(cur);
		    Integer uid = JUserCallService.createUserCall(cur);
		    JUserCallService.UpdateTradingPwd(uid, "123abc");
		    UpdateTradingPwdBean updadetradingpwdbean = new UpdateTradingPwdBean();
		    UpdateTradingPwdCall updatetradingpwdcall = new UpdateTradingPwdCall();
		    updadetradingpwdbean.setAType("ByUserId");
		    updadetradingpwdbean.setLoginMobile(cur.getMobile());
		    updadetradingpwdbean.setPasswordType(1);
		    String opwd = DesUtil.encrypt("123abc",DesUtil.DES_KEY_STRING,DesUtil.DES_IVPARAMETER);
		    String pwd = DesUtil.encrypt("abc123",DesUtil.DES_KEY_STRING,DesUtil.DES_IVPARAMETER);
		    updadetradingpwdbean.setNewPassword(pwd);
		    updadetradingpwdbean.setOldPassword(opwd);
		    updadetradingpwdbean.setPasActionType("ModifyTradingPassword");
		    updadetradingpwdbean.setUserId(uid);
		    updatetradingpwdcall.setData(updadetradingpwdbean);
		    updatetradingpwdcall.callService();
	
			passwordrequestdtoBean.setUserId(uid);
			passwordrequestdtoBean.setNewPassword("abc123");
			passwordrequestdtoCall.setData(FormatUtil.beanToHttpString(passwordrequestdtoBean,true,false));
			//passwordrequestdtoCall.setData(passwordrequestdtoBean);
			passwordrequestdtoCall.callService();			
			
			Logger.verifyEquals("1", passwordrequestdtoCall.getString("Result"), "验证Result");
			Logger.verifyEquals("true", passwordrequestdtoCall.getString("Success"), "验证Success");		
			Logger.verifyEquals("200", passwordrequestdtoCall.getString("ResponseCode"), "验证ResponseCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_TradingPasswordRequestDto_008() {
		Logger.start(true,"验证交易密码,密码加密2,成功");
		try {
			CreateUserReq cur = new CreateUserReq();
		    cur = JUserCallService.initCreateUserBean(cur);
		    Integer uid = JUserCallService.createUserCall(cur);
		    String npwd="abc123";
		    UpdateTradingPwdBean updadetradingpwdbean = new UpdateTradingPwdBean();
		    UpdateTradingPwdCall updatetradingpwdcall = new UpdateTradingPwdCall();
		    updadetradingpwdbean.setAType("ByUserId");
		    updadetradingpwdbean.setLoginMobile(cur.getMobile());
		    updadetradingpwdbean.setPasswordType(2);
		    byte[] rsabytes = RSAUtils.encryptByPublicKey(npwd.getBytes(),
					(RSAPublicKey) RSAUtils.loadPublicKey(EnvSetup
							.getData("USER_PUBLIC_KEY")));
			String rsanpwd = Base64.getEncoder().encodeToString(rsabytes);
		    updadetradingpwdbean.setNewPassword(rsanpwd);
		    updadetradingpwdbean.setOldPassword(null);
		    updadetradingpwdbean.setPasActionType("2");
		    updadetradingpwdbean.setUserId(uid);
		    updatetradingpwdcall.setData(updadetradingpwdbean);
		    updatetradingpwdcall.callService();
			Logger.verifyEquals("true", updatetradingpwdcall.getString("Success"), "验证Success");		
			Logger.verifyEquals("200", updatetradingpwdcall.getString("ResponseCode"), "验证ResponseCode");
			
			passwordrequestdtoBean.setUserId(uid);
			passwordrequestdtoBean.setNewPassword(npwd);
			passwordrequestdtoCall.setData(FormatUtil.beanToHttpString(passwordrequestdtoBean,true,false));
			//passwordrequestdtoCall.setData(passwordrequestdtoBean);
			passwordrequestdtoCall.callService();			
			
			Logger.verifyEquals("1", passwordrequestdtoCall.getString("Result"), "验证Result");
			Logger.verifyEquals("true", passwordrequestdtoCall.getString("Success"), "验证Success");		
			Logger.verifyEquals("200", passwordrequestdtoCall.getString("ResponseCode"), "验证ResponseCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_TradingPasswordRequestDto_009() {
		Logger.start(true,"验证交易密码,密码加密2,密文验证成功");
		try {
			CreateUserReq cur = new CreateUserReq();
		    cur = JUserCallService.initCreateUserBean(cur);
		    Integer uid = JUserCallService.createUserCall(cur);
		    String npwd="abc123";
		    UpdateTradingPwdBean updadetradingpwdbean = new UpdateTradingPwdBean();
		    UpdateTradingPwdCall updatetradingpwdcall = new UpdateTradingPwdCall();
		    updadetradingpwdbean.setAType("ByUserId");
		    updadetradingpwdbean.setLoginMobile(cur.getMobile());
		    updadetradingpwdbean.setPasswordType(2);
		    byte[] rsabytes = RSAUtils.encryptByPublicKey(npwd.getBytes(),
					(RSAPublicKey) RSAUtils.loadPublicKey(EnvSetup
							.getData("USER_PUBLIC_KEY")));
			String rsanpwd = Base64.getEncoder().encodeToString(rsabytes);
		    updadetradingpwdbean.setNewPassword(rsanpwd);
		    updadetradingpwdbean.setOldPassword(null);
		    updadetradingpwdbean.setPasActionType("2");
		    updadetradingpwdbean.setUserId(uid);
		    updatetradingpwdcall.setData(updadetradingpwdbean);
		    updatetradingpwdcall.callService();
			Logger.verifyEquals("true", updatetradingpwdcall.getString("Success"), "验证Success");		
			Logger.verifyEquals("200", updatetradingpwdcall.getString("ResponseCode"), "验证ResponseCode");
			
			passwordrequestdtoBean.setPasswordType(2);
			passwordrequestdtoBean.setUserId(uid);
			passwordrequestdtoBean.setNewPassword(rsanpwd);
			passwordrequestdtoCall.setData(FormatUtil.beanToHttpString(passwordrequestdtoBean,true,false));
			//passwordrequestdtoCall.setData(passwordrequestdtoBean);
			passwordrequestdtoCall.callService();			
			
			Logger.verifyEquals("1", passwordrequestdtoCall.getString("Result"), "验证Result");
			Logger.verifyEquals("true", passwordrequestdtoCall.getString("Success"), "验证Success");		
			Logger.verifyEquals("200", passwordrequestdtoCall.getString("ResponseCode"), "验证ResponseCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_TradingPasswordRequestDto_010() {
		Logger.start(true,"验证交易密码,密码加密2,错误密码,密文验证失败");
		try {
			CreateUserReq cur = new CreateUserReq();
		    cur = JUserCallService.initCreateUserBean(cur);
		    Integer uid = JUserCallService.createUserCall(cur);
		    String npwd="abc123";
		    UpdateTradingPwdBean updadetradingpwdbean = new UpdateTradingPwdBean();
		    UpdateTradingPwdCall updatetradingpwdcall = new UpdateTradingPwdCall();
		    updadetradingpwdbean.setAType("ByUserId");
		    updadetradingpwdbean.setLoginMobile(cur.getMobile());
		    updadetradingpwdbean.setPasswordType(2);
		    byte[] rsabytes = RSAUtils.encryptByPublicKey(npwd.getBytes(),
					(RSAPublicKey) RSAUtils.loadPublicKey(EnvSetup
							.getData("USER_PUBLIC_KEY")));
			String rsanpwd = Base64.getEncoder().encodeToString(rsabytes);
		    updadetradingpwdbean.setNewPassword(rsanpwd);
		    updadetradingpwdbean.setOldPassword(null);
		    updadetradingpwdbean.setPasActionType("2");
		    updadetradingpwdbean.setUserId(uid);
		    updatetradingpwdcall.setData(updadetradingpwdbean);
		    updatetradingpwdcall.callService();
			Logger.verifyEquals("true", updatetradingpwdcall.getString("Success"), "验证Success");		
			Logger.verifyEquals("200", updatetradingpwdcall.getString("ResponseCode"), "验证ResponseCode");
			
			passwordrequestdtoBean.setPasswordType(2);
			passwordrequestdtoBean.setUserId(uid);
			passwordrequestdtoBean.setNewPassword(1+rsanpwd);
			passwordrequestdtoCall.setData(FormatUtil.beanToHttpString(passwordrequestdtoBean,true,false));
			//passwordrequestdtoCall.setData(passwordrequestdtoBean);
			passwordrequestdtoCall.callService();			
			
			Logger.verifyEquals("0", passwordrequestdtoCall.getString("Result"), "验证Result");
			Logger.verifyEquals("false", passwordrequestdtoCall.getString("Success"), "验证Success");		
			Logger.verifyEquals("400", passwordrequestdtoCall.getString("ResponseCode"), "验证ResponseCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}